package com.test.annotation;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * Created by jl on 2016/4/21.
 */
public class Demo {
    public static void main(String[] args) {
        Cls1 cls1 = new Cls1();
//        cls1.test();
        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock();
        ReentrantReadWriteLock.ReadLock readLock = lock.readLock();
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                boolean b = readLock.tryLock();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("获取到读锁了：" + b);
            }
        });
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                boolean b = writeLock.tryLock();
                System.out.println("获得写锁了：" + b);
            }
        });
        thread1.start();
        thread2.start();
    }
}
